#include "SleepableCoroutine.h"
using namespace std;
using namespace boost;
using namespace boost::coroutines;

namespace CggUtil {

bool __corSleepCB(void* params, int& interval) {
    bool ret = false;
    CorType::pull_type* cor = static_cast<CorType::pull_type*>(params);
    assert(cor != NULL);

    // get parameters
    auto corArgs = cor->get();
    interval = std::get<0>(corArgs);
    const CorUtilCB corUntilCB = std::get<1>(corArgs);

    // switch to coroutine process, make it continue
    bool isCompleted = false;
    if (corUntilCB == nullptr || corUntilCB()) {
        isCompleted = !(*cor)();
    }

    if (isCompleted || interval <= 0) {
        delete cor;
        interval = INVALID_TIMER_INTERV;
    } else {
        ret = true;
    }
    return ret;
}

} // namespace CggUtil